﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Chinaer.Dao.EF
{
    public class DingDanStatusDAO : Repository<Model.Entities.Model_DingDanStatus>, Chinaer.IDao.IDingDanStatusDAO
    {
        public override bool Add(Model.Entities.Model_DingDanStatus entity)
        {
            return base.Add(entity);
        }
        public override bool Delete(Model.Entities.Model_DingDanStatus entity)
        {
            if (entity == null)
            {
                Chinaer.Core.Helper.ExceptonHelper.ThrowException("Model_DingDanStatus 删除的实体为null");
            }
            entity.IsDelete = true;
            return base.Update(entity);
            // return base.Delete(entity);
        }
        public override bool Delete(string id)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                Model.Entities.Model_DingDanStatus model_DingDanStatus = new Model.Entities.Model_DingDanStatus();
                model_DingDanStatus = context.DingDanStatus.FirstOrDefault(p => p.IsUse == true && p.IsDelete == false && p.ID.Equals(id, StringComparison.CurrentCultureIgnoreCase));
                if (model_DingDanStatus == null)
                {
                    Chinaer.Core.Helper.ExceptonHelper.ThrowException("DingDanStatus 删除出错");
                }
                model_DingDanStatus.IsDelete = true;
                model_DingDanStatus.IsUse = false;
                return base.Update(model_DingDanStatus);
            }
        }
        public override int DeleteByIDs(IList<string> listID)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                var queryResult = from a in context.DingDanStatus
                                  where listID.Contains(a.ID)
                                  select a;
                lock (queryResult)
                {
                    foreach (var item in queryResult)
                    {
                        context.Entry<Model.Entities.Model_DingDanStatus>(item).State = System.Data.Entity.EntityState.Modified;
                        item.IsDelete = true;
                    }
                }
                return context.SaveChanges();
            }
        }
        public override bool Exists(string id)
        {
            return GetByID(id) == null ? false : true;
        }
        public override Model.Entities.Model_DingDanStatus GetByID(string id)
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                return context.DingDanStatus.FirstOrDefault(a => a.ID == id && a.IsDelete == false && a.IsUse == true);
            }
        }
        public override IList<Model.Entities.Model_DingDanStatus> Query(string sql, int pageIndex, out int recordCount, int pageSize, params System.Data.SqlClient.SqlParameter[] sqlParams)
        {
            return base.Query(sql, pageIndex, out recordCount, pageSize, sqlParams);
        }
        public override bool Update(Model.Entities.Model_DingDanStatus entity)
        {
            if (GetByID(entity.ID) == null)
            {
                Chinaer.Core.Helper.ExceptonHelper.ThrowException("DingDanStatus 更新一个不存在的实体");
            }
            return base.Update(entity);
        }


        public Model.Entities.Model_DingDanStatus GetDingDanStatusByDingDanID(string dingDanID)
        {
            IDao.IDingDanDAO dingDanDAO = new Dao.EF.DingDanDAO();
            Model.Entities.Model_DingDan model_DingDan = dingDanDAO.GetByID(dingDanID);
            if (model_DingDan != null)
            {
                string dingDanStatusID = model_DingDan.DingDanStatusID;
                Model.Entities.Model_DingDanStatus model_DingDanStatus = GetByID(dingDanStatusID);
                return model_DingDanStatus;
            }
            return null;
        }


        public List<Model.Entities.Model_DingDanStatus> GetAllDingDanStatus()
        {
            using (ChinaerContext context = ChinaerContext.GetNewInstance())
            {
                return context.DingDanStatus.Distinct().ToList();
            }
        }
    }
}
